package com.cloud.phone.task;

import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.cloud.phone.base.BaseApp;
import com.cloud.phone.constants.Constants;
import com.cloud.phone.task.bean.BeAttentionChatBean;
import com.cloud.phone.task.bean.ChatMsgList;
import com.cloud.phone.task.bean.CommentBean;
import com.cloud.phone.task.bean.MsgTaskBean;
import com.cloud.phone.task.bean.ServerInnerDataBean;
import com.cloud.phone.task.bean.ServerTaskDataBean;
import com.cloud.phone.task.bean.ServerTaskInnerBean;
import com.cloud.phone.task.taskCongfig.TaskTypeConfig;
import com.cloud.phone.task.taskCongfig.WhatsAppTypeConfig;
import com.cloud.phone.task.tiktok.TikTokTask;
import com.cloud.phone.tools.AccessibilityTool;
import com.cloud.phone.tools.CmdTool;
import com.cloud.phone.tools.LogUtils;
import com.cloud.phone.tools.MyUtils;
import com.cloud.phone.utils.AppUtils;
import com.cloud.phone.utils.MediaRefreshHelper;
import com.cloud.phone.utils.PermissionUtils;
import com.cloud.phone.view.TsUtils;
import com.cloud.phone.websocket.WebsocketUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 任务消息处理器
 */
public class MsgTaskManger {
    Context activity;

    public MsgTaskManger(Context context) {
        this.activity = context;
    }


    /**
     * 模拟服务端发送消息
     */
    public void mockServerSendData(String taskType) {
        if (MyUtils.isEmpty(taskType)) {
            createUpdatePhoneInfoTask();
            LogUtils.i("消息为空,手动执行更新数据任务");
            return;
        }
        onHandleMsg(mockData(taskType));
    }


    /**
     * 手动创建一个更新数据的任务
     */
    public void createUpdatePhoneInfoTask(){
        ServerTaskDataBean serverTaskDataBean = new ServerTaskDataBean();
        serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_UPDATE_CURRENT_APP_LOGIN_INFO);
        TaskManager.getInstance().createTikTokTask(serverTaskDataBean);
        showLog("执行手动任务:" + "创建一个任务执行");
        BaseTask currentTask = TaskManager.getInstance().getCurrentTask();
        if (currentTask != null && currentTask.service != null) {
            currentTask.service.needLoopUpdatePhoneInfo = false;
            String pkgName = AccessibilityTool.getInstance().getCurrentPagePackageName(currentTask.service);
            if (!Constants.tiktokPackageName.equals(pkgName)) {
                TaskManager.getInstance().onOpenTaskApp(currentTask,Constants.tiktokPackageName);
            }
        }
    }


    public void createWhatsAppTask(){
        ServerTaskDataBean serverTaskDataBean = new ServerTaskDataBean();
        serverTaskDataBean.setTaskType(WhatsAppTypeConfig.WHATS_TYPE_GET_MESSAGE_HISTORY);
        TaskManager.getInstance().createWhatsTask(serverTaskDataBean);
        showLog("创建手动任务:" + "创建了一个任务执行");
        BaseTask currentTask = TaskManager.getInstance().getCurrentTask();
        if (currentTask != null && currentTask.service != null) {
            currentTask.service.needLoopUpdatePhoneInfo = false;
            String pkgName = AccessibilityTool.getInstance().getCurrentPagePackageName(currentTask.service);
            if (!Constants.whatsAppPackageName.equals(pkgName)) {
                TaskManager.getInstance().onOpenTaskApp(currentTask,Constants.whatsAppPackageName);
            }
        }
    }


    static long dealRegisterTime;
    public static synchronized void dealWithAutoRegisterLauncher(BaseTask currentTask) {
        if(currentTask==null){
            TaskManager.isRegisterTaskDoing=false;
            return;
        }
        TikTokTask tikTokTask= (TikTokTask) currentTask;
        if(tikTokTask.isResetTikTok||TaskManager.isRegisterTaskDoing){
            showLog("已初始化tiktok或正在执行初始化Tiktok任务，无需再次执行");
            return;
        }
        TaskManager.isRegisterTaskDoing=true;
        dealRegisterTime = System.currentTimeMillis();
        TsUtils.showTips("切回业务APP");
        currentTask.service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
        CmdTool.getInstance().killApp(Constants.tiktokPackageName);
        currentTask.showLogAndTsPrint("初始化Tiktok环境，准备注册任务");
        CmdTool.getInstance().doCmd("pm clear " + Constants.tiktokPackageName);
        if(Looper.getMainLooper()==Looper.myLooper()) {
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    AppUtils.Companion.launchApp(BaseApp.app.getApplicationContext(), Constants.tiktokPackageName);
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            String pkgName = AccessibilityTool.getInstance().getCurrentPagePackageName(currentTask.service);
                            if(Constants.tiktokPackageName.equals(pkgName)) {
                                TikTokTask tikTokTask = (TikTokTask) currentTask;
                                tikTokTask.isResetTikTok = true;
                                currentTask.taskStatus = BaseTask.ON_START_TASK;
                                TaskManager.isRegisterTaskDoing = false;
                            }else{
                                TaskManager.isRegisterTaskDoing = false;
                                TikTokTask tikTokTask = (TikTokTask) currentTask;
                                tikTokTask.isResetTikTok = false;
                                currentTask.taskStatus = BaseTask.ON_START_TASK;
                            }
                        }
                    }, 3000);
                }
            }, 2000);
        }else {
            try {
                Thread.sleep(2000);
                AppUtils.Companion.launchApp(BaseApp.app.getApplicationContext(), Constants.tiktokPackageName);
                Thread.sleep(3000);
                String pkgName = AccessibilityTool.getInstance().getCurrentPagePackageName(currentTask.service);
                if(Constants.tiktokPackageName.equals(pkgName)) {
                    tikTokTask.isResetTikTok = true;
                    currentTask.taskStatus = BaseTask.ON_START_TASK;
                    TaskManager.isRegisterTaskDoing = false;
                }else{
                    TaskManager.isRegisterTaskDoing=false;
                    tikTokTask.isResetTikTok = false;
                    currentTask.taskStatus = BaseTask.ON_START_TASK;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public String mockData(String taskType) {
        MsgTaskBean msgTaskBean = new MsgTaskBean();
        msgTaskBean.setCode(1003);
        msgTaskBean.setToken(SPUtils.getInstance().getString("token"));
        ServerTaskInnerBean serverTaskInnerBean = new ServerTaskInnerBean();
        serverTaskInnerBean.setLink("https://www.tiktok.com/@large.cole/video/7353433041668164907?_t=8lqMpZQOh94&_r=1");
        serverTaskInnerBean.setWaitTimeLike(14);
        serverTaskInnerBean.setWaitTimeComment(14);
        serverTaskInnerBean.setDuration(1);
//        serverTaskInnerBean.setAccount("yin.d27");
//        serverTaskInnerBean.setAccount("infamous.belle1152");
        serverTaskInnerBean.setAccount("qqqqdou1vk");
        List<String> keywords = new ArrayList<>();
        keywords.add("富贵");
        serverTaskInnerBean.setKeyword(keywords);
        CommentBean commentBean = new CommentBean();
        commentBean.setMsg("我是评论");
        commentBean.setLink("https://www.tiktok.com/@large.cole/video/7353433041668164907?_t=8lqMpZQOh94&_r=1");
        serverTaskInnerBean.setComment(commentBean);
        serverTaskInnerBean.setFollowAccount("yabear2024");
        serverTaskInnerBean.setFilepath("全部");
        serverTaskInnerBean.setTaskType(TaskTypeConfig.SERVER_UPDATE_ACCOUNT);
        ServerInnerDataBean serverInnerDataBean = new ServerInnerDataBean();
        if (!MyUtils.isEmpty(taskType)) {
            int taskIndex = Integer.parseInt(taskType);
            if (taskIndex == 1) {
                serverTaskInnerBean.setTaskType(TaskTypeConfig.SERVER_TASK_LIKE_SINGLE_WORK);
                msgTaskBean.setTaskType(TaskTypeConfig.SERVER_TASK_LIKE_SINGLE_WORK);
            }
            if (taskIndex == 2) {
                serverTaskInnerBean.setTaskType(TaskTypeConfig.SERVER_TASK_ATTENTION_SINGLE_USER);
                msgTaskBean.setTaskType(TaskTypeConfig.SERVER_TASK_ATTENTION_SINGLE_USER);
            }
            if (taskIndex == 3) {
                serverTaskInnerBean.setTaskType(TaskTypeConfig.SERVER_TASK_COMMENT_SINGLE_WORK);
                msgTaskBean.setTaskType(TaskTypeConfig.SERVER_TASK_COMMENT_SINGLE_WORK);
            }
            if (taskIndex == 4) {
                serverTaskInnerBean.setTaskType(TaskTypeConfig.SERVER_SEND_MSG);
                msgTaskBean.setTaskType(TaskTypeConfig.SERVER_SEND_MSG);
            }
            if (taskIndex == 5) {
                serverTaskInnerBean.setTaskType(TaskTypeConfig.SERVER_TASK_PUBLISH_WORKS);
                msgTaskBean.setTaskType(TaskTypeConfig.SERVER_TASK_PUBLISH_WORKS);
            }
            if (taskIndex == 6) {
                serverTaskInnerBean.setTaskType(TaskTypeConfig.SERVER_TASK_EDIT_USER_INFO);
                msgTaskBean.setTaskType(TaskTypeConfig.SERVER_TASK_EDIT_USER_INFO);
            }
            if (taskIndex == 7) {
                serverTaskInnerBean.setTaskType(TaskTypeConfig.TIKTOK_UPDATE_CURRENT_APP_LOGIN_INFO);
                msgTaskBean.setTaskType(TaskTypeConfig.SERVER_UPDATE_ACCOUNT);
            }
        }
        serverInnerDataBean.setJson(GsonUtils.toJson(serverTaskInnerBean));
        msgTaskBean.setData(serverInnerDataBean);
        return GsonUtils.toJson(msgTaskBean);
    }


    public void onChangeToken(){
        TsUtils.showTips("更换Token");
        WebsocketUtil websocketUtil = WebsocketUtil.getWebsocketUtil();
        if(WebsocketUtil.getWebsocketUtil().mWebSocketManager!=null) {
            WebsocketUtil.getWebsocketUtil().mWebSocketManager.disConnect();
        }
        websocketUtil.initHttpConfig();
    }


    public void onHandleMsg(String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        try {
            MsgTaskBean msgTaskBean = GsonUtils.fromJson(msg, MsgTaskBean.class);
            if(msgTaskBean==null){
                return;
            }
            int code=msgTaskBean.getCode();
            Integer type=msgTaskBean.getType();
            if(code == 1003&&Objects.equals(TaskTypeConfig.TIKTOK_TASK_AUTO_LOGIN,type)){
                BaseTask currentTask = TaskManager.getInstance().getCurrentTask();
                if(currentTask==null){
                    return;
                }
                showLog(msg);
                if(currentTask instanceof TikTokTask&&currentTask.currentTaskType==TaskTypeConfig.TIKTOK_TASK_AUTO_LOGIN){
                    TikTokTask tikTokTask= (TikTokTask) currentTask;
                    String emailCode=msgTaskBean.getData().getJson();
                    tikTokTask.setCurrentEmailCode(emailCode);
                }else{
                    currentTask.onTaskErr("登录账号任务状态异常");
                }
                return;
            }
            if(code==1001){
                WebsocketUtil.getWebsocketUtil().updatePhoneStatus();
            }
            if(code==9999){
                onChangeToken();
                return;
            }
            if (code != 1003) {
                if(code!=1004&&!Objects.equals(TaskTypeConfig.SERVER_STOP_ALL_TASK,type)){
                    //判断是否为强制任务
                    if(isForceTask()){
                        TikTokTask tikTokTask= (TikTokTask) TaskManager.getInstance().getCurrentTask();
                        if(tikTokTask==null){
                            return;
                        }
                        PermissionUtils.startService();
                        if(tikTokTask.tickTokTaskDealTools!=null) {
                            tikTokTask.taskStatus=BaseTask.STOP;
                            tikTokTask.tickTokTaskDealTools.forceStop();
                        }
                        BaseTask task = TaskManager.getInstance().getCurrentTask();
                        task.onTaskStop();
                        //TODO 停止当前任务后执行强制任务
                    }
                    return;
                }
            }
            showLog("消息接收成功，处理消息：type="+type);
            BaseTask task = TaskManager.getInstance().getCurrentTask();
            if(type!=null&&type!=TaskTypeConfig.SERVER_STOP_ALL_TASK){
                if (task != null&&task.service!=null&&task.taskStatus!=TikTokTask.ERR) {
                    if(task.service.isWorking){
                        LogUtils.e("本地已经有任务正在执行了！！！！");
                        task.onTaskErr("已有任务在执行了,taskType=" + task.getServerTaskTypeByCurrentTaskType());
                        return;
                    }
                }
            }
            TaskManager.getInstance().currentTiktokUserInfo = null;
            createTaskInTaskManager(msgTaskBean);
        } catch (Exception e) {
            e.printStackTrace();
            showLog("数据解析异常-->" + e.getMessage() + "\n" + e.getCause());
            LogUtils.e(msg);
            BaseTask task = TaskManager.getInstance().getCurrentTask();
            if(task!=null){
                task.onTaskErr("任务接收异常");
            }
        }
    }


    public static void showLog(String logStr){
        Message message = Message.obtain();
        message.obj = logStr;
        EventBus.getDefault().post(message);
    }

    private boolean isForceTask() {
        return false;
    }


    public void createTaskInTaskManager(MsgTaskBean taskBean) {
        synchronized (taskBean) {
            ServerTaskDataBean serverTaskBean = createTaskDataBeanByTaskType(taskBean);//根据服务端返回内容创建数据
            if(serverTaskBean.getTaskType()==TaskTypeConfig.SERVER_BE_ATTENTION_REPLAY){
                return;
            }
            if(taskBean.getTaskType()==TaskTypeConfig.SERVER_STOP_ALL_TASK){
                TikTokTask tikTokTask= (TikTokTask) TaskManager.getInstance().getCurrentTask();
                PermissionUtils.startService();
                if(tikTokTask!=null&&tikTokTask.tickTokTaskDealTools!=null) {
                    tikTokTask.taskStatus=BaseTask.STOP;
                    tikTokTask.tickTokTaskDealTools.forceStop();
                    TaskManager.getInstance().createTikTokTask(serverTaskBean);
                }else{
                    TaskManager.getInstance().createTikTokTask(serverTaskBean);
                    BaseTask task = TaskManager.getInstance().getCurrentTask();
                    task.taskStatus=BaseTask.STOP;
                }
                BaseTask task = TaskManager.getInstance().getCurrentTask();
                task.onTaskStop();
                return;
            }
            showLog("准备启动服务并打开app");
            //根据服务端的task对象创建一个本地的任务对象
            if (serverTaskBean.getTaskType() != 0) {
                PermissionUtils.startService();
                showLog("打开服务成功");
                if(taskBean.getTaskType()==20000){
                    TaskManager.getInstance().createWhatsTask(serverTaskBean);
                    showLog("准备打开whatsApp");
                    openApplicationByWhatsTaskType(serverTaskBean);
                    showLog("打开whatsApp完成");
                }else{
                    BaseTask task = TaskManager.getInstance().createTikTokTask(serverTaskBean);
                    showLog("准备打开app:\n"+task.currentTaskType+"/serverTaskType="+serverTaskBean.getTaskType());
                    openApplicationByTaskType(serverTaskBean);
                    showLog("打开app完成");
                }
            }
        }
    }



    /**
     * {"magic":null,"code":1003,"data":{"id":"664313988fa2425999aec993","mainId":"664313988fa2425999aec990","json":"{\"taskType\":10006,\"phoneId\":null,\"cpPhoneId\":4297481541262544897,\"account\":\"infamous.belle1152\",\"mainId\":\"664313988fa2425999aec990\",\"waitTime\":0,\"followAccount\":\"sdadad\",\"maxFollow\":0,\"msgList\":[\"xxxxx\",\"11122\"]}"},"taskType":10006,"token":null,"type":null}]
     * @param taskBean
     * @return
     */
    private ServerTaskDataBean createTaskDataBeanByTaskType(MsgTaskBean taskBean) {
        ServerTaskDataBean serverTaskDataBean = new ServerTaskDataBean();
        if (taskBean.getData() != null && taskBean.getData().getInnerBean() != null) {
            ServerTaskInnerBean serverTaskInnerBean = taskBean.getData().getInnerBean();
            //将账号信息设置进去
            if (MyUtils.isEmpty(serverTaskInnerBean.getAccount())) {
                serverTaskDataBean.setAccountWithUserName("infamous.belle1152");
//                serverTaskDataBean.setAccountWithUserName("yin.d27");
            } else {
                serverTaskDataBean.setAccountWithUserName(serverTaskInnerBean.getAccount());
            }
            serverTaskDataBean.setId(taskBean.getData().getId());
            serverTaskDataBean.setServerTaskType(taskBean.getTaskType());
            serverTaskDataBean.setMainId(taskBean.getData().getMainId());
            serverTaskDataBean.setPhoneId(taskBean.getData().getInnerBean().getPhoneId());
            serverTaskDataBean.setCpPhoneId(taskBean.getData().getInnerBean().getCpPhoneId());
            serverTaskDataBean.setWaitTime(serverTaskInnerBean.getWaitTime() * 1000);
            serverTaskDataBean.setFollowType(serverTaskInnerBean.getFollowType());
            serverTaskDataBean.setMinInterval(serverTaskInnerBean.getMinInterval());
            serverTaskDataBean.setMaxInterval(serverTaskInnerBean.getMaxInterval());
            serverTaskDataBean.setPublishTitle(serverTaskInnerBean.getTitle());

            if(!MyUtils.isEmpty(serverTaskInnerBean.getAvatar())) {
                new MediaRefreshHelper(BaseApp.app).scanFolder(new File(serverTaskInnerBean.getAvatar()));
            }
            if(!MyUtils.isEmpty(serverTaskInnerBean.getFilepath())) {
                new MediaRefreshHelper(BaseApp.app).scanFolder(new File(serverTaskInnerBean.getFilepath()));
            }
            switch (taskBean.getTaskType()) {
                case WhatsAppTypeConfig.WHATS_TYPE_UPDATE_USER_INFO:
                    serverTaskDataBean.setTaskType(WhatsAppTypeConfig.WHATS_TYPE_UPDATE_USER_INFO);
                    break;
                //关键词/普通-养号任务
                case TaskTypeConfig.SERVER_UPDATE_ACCOUNT:
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_UPDATE_CURRENT_APP_LOGIN_INFO);
                    break;
                case TaskTypeConfig.SERVER_TASK_KEEP_ACCOUNT:
                    if (MyUtils.isEmpty(serverTaskInnerBean.getKeyword())) {
                        serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_AUTO_LOOK_VIDEO);
                    } else {
                        serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_SEARCH_AUTO_LOOK_VIDEO);
                        serverTaskDataBean.setSearchKeywords(serverTaskInnerBean.getKeyword());
                    }
                    serverTaskDataBean.setAttentionPercent(10);
                    serverTaskDataBean.setCommentPercent(5);
                    serverTaskDataBean.setLikePercent(20);
                    serverTaskDataBean.setDuration(serverTaskInnerBean.getDuration());
                    break;
                case TaskTypeConfig.SERVER_TASK_LIKE_SINGLE_WORK:
                    serverTaskDataBean.setLikePercent(100);
                    serverTaskDataBean.setDuration(serverTaskInnerBean.getWaitTimeLike() * 1000);
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_SINGLE_WORKS_LIKE_BY_LINK);
                    serverTaskDataBean.setVideoLink(serverTaskInnerBean.getLink());
                    break;
                case TaskTypeConfig.SERVER_TASK_COMMENT_SINGLE_WORK:
                    if (serverTaskInnerBean.getComment() != null) {
                        serverTaskDataBean.setCommentText(serverTaskInnerBean.getComment().getMsg());
                        serverTaskDataBean.setVideoLink(serverTaskInnerBean.getComment().getLink());
                    }
                    serverTaskDataBean.setDuration(serverTaskInnerBean.getWaitTimeComment() * 1000);
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_SINGLE_WORKS_COMMENT_BY_LINK);
                    break;
                case TaskTypeConfig.SERVER_TASK_ATTENTION_SINGLE_USER:
                    serverTaskDataBean.setDealWithUserName(serverTaskInnerBean.getFollowAccount());
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_ATTENTION_USER);
                    break;
                case TaskTypeConfig.SERVER_SEND_MSG:
                    serverTaskDataBean.setDealWithUserName(serverTaskInnerBean.getFollowAccount());
                    serverTaskDataBean.setChatWithOtherMsgList(serverTaskInnerBean.getPrivateMsg());
                    serverTaskDataBean.setMsgList(serverTaskInnerBean.getMsgList());
                    serverTaskDataBean.setFollowAccount(serverTaskInnerBean.getFollowAccount());
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_SINGLE_CHAT_TO_USER);
                    break;
                case TaskTypeConfig.SERVER_GET_USER_FANS_TO_CHAT_LIST:
                    serverTaskDataBean.setFollowAccount(serverTaskInnerBean.getFollowAccount());
                    serverTaskDataBean.setMaxFollowCount(serverTaskInnerBean.getMaxFollowCount());
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_GET_CHAT_TO_USER_FANS_LIST);
                    break;
                case TaskTypeConfig.SERVER_TASK_GET_USER_FANS_LIST:
                    serverTaskDataBean.setFollowAccount(serverTaskInnerBean.getFollowAccount());
                    serverTaskDataBean.setMaxFollowCount(serverTaskInnerBean.getMaxFollowCount());
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_GET_USER_FAN_LIST);
                    break;
                case TaskTypeConfig.SERVER_TASK_EDIT_USER_INFO:
                    serverTaskDataBean.setIntroduction(serverTaskInnerBean.getIntroduction());
                    serverTaskDataBean.setAvatar(serverTaskInnerBean.getAvatar());
//                    serverTaskDataBean.setAvatar("全部");
                    serverTaskDataBean.setNickName(serverTaskInnerBean.getNickName());
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_CHANGE_PERSONAL_INFO);
                    break;
                case TaskTypeConfig.SERVER_TASK_PUBLISH_WORKS:
                    serverTaskDataBean.setFileDirName(serverTaskInnerBean.getFilepath());
//                    serverTaskDataBean.setFileDirName("全部");
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_AUTO_PUBLISH_WORKS);
                    break;
                case TaskTypeConfig.SERVER_TASK_REGISTER_ACCOUNT:
                    TaskManager.isRegisterTaskDoing=false;
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_AUTO_REGISTER);
                    serverTaskDataBean.setAccountId(serverTaskInnerBean.getAccountId());
                    serverTaskDataBean.setPassword(serverTaskInnerBean.getPassword());
                    break;
                case TaskTypeConfig.SERVER_TASK_LOGIN:
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_TASK_AUTO_LOGIN);
                    serverTaskDataBean.setAccountId(serverTaskInnerBean.getAccountId());
                    serverTaskDataBean.setPassword(serverTaskInnerBean.getPassword());
                    break;
                case TaskTypeConfig.SERVER_BE_ATTENTION_REPLAY:
                    LogUtils.i("被关注自动回复数据更新成功");
                    ChatMsgList chatMsgBean = serverTaskInnerBean.getPrivateMsg();
                    if(chatMsgBean!=null){
                        String beAttentionChatStr=SPUtils.getInstance().getString(BeAttentionChatBean.class.getSimpleName());
                        BeAttentionChatBean beAttentionChatBean;
                        if(!MyUtils.isEmpty(beAttentionChatStr)){
                            beAttentionChatBean=GsonUtils.fromJson(beAttentionChatStr,BeAttentionChatBean.class);
                            if(beAttentionChatBean==null){
                                beAttentionChatBean=new BeAttentionChatBean();
                            }
                        }else{
                            beAttentionChatBean=new BeAttentionChatBean();
                        }
                        boolean needUpdate=false;
                        for(int i=0;i<beAttentionChatBean.getBeAttentionChatListBeans().size();i++){
                            if(beAttentionChatBean.getBeAttentionChatListBeans().get(i).getAccount().equals(chatMsgBean.getAccount())){
                                beAttentionChatBean.getBeAttentionChatListBeans().set(i,chatMsgBean);
                                if(MyUtils.isEmpty(chatMsgBean.getPrivateMsgList())){
                                    beAttentionChatBean.getBeAttentionChatListBeans().remove(i);
                                }
                                LogUtils.w("更新已有数据为-->"+GsonUtils.toJson(chatMsgBean));
                                needUpdate=true;
                                break;
                            }
                        }
                        if(!needUpdate){
                            LogUtils.w("加入新数据-->"+GsonUtils.toJson(chatMsgBean));
                            beAttentionChatBean.getBeAttentionChatListBeans().add(chatMsgBean);
                        }
                        SPUtils.getInstance().put(BeAttentionChatBean.class.getSimpleName(),GsonUtils.toJson(beAttentionChatBean));
                        LogUtils.w("保存或更新新的数据"+GsonUtils.toJson(beAttentionChatBean));
                        taskBean.getData().setStatus(2);
                        taskBean.setCode(1006);
                        WebsocketUtil.getWebsocketUtil().sendMsg(GsonUtils.toJson(taskBean));
                    }else{
                        taskBean.getData().setStatus(3);
                        taskBean.setCode(1006);
                        WebsocketUtil.getWebsocketUtil().sendMsg(GsonUtils.toJson(taskBean));
                    }
                    break;
            }
        }else{
            if(taskBean.getData()!=null&&taskBean.getData().getInnerBean()==null){
                //单独处理更新任务
                if(taskBean.getTaskType()==TaskTypeConfig.SERVER_UPDATE_ACCOUNT) {
                    serverTaskDataBean.setTaskType(TaskTypeConfig.TIKTOK_UPDATE_CURRENT_APP_LOGIN_INFO);
                    serverTaskDataBean.setId(taskBean.getData().getId());
                    serverTaskDataBean.setServerTaskType(taskBean.getTaskType());
                    serverTaskDataBean.setMainId(taskBean.getData().getMainId());
                }else if(taskBean.getTaskType()==TaskTypeConfig.SERVER_STOP_ALL_TASK){
                    serverTaskDataBean.setTaskType(TaskTypeConfig.STOP_CURRENT_TASK);
                    serverTaskDataBean.setId(taskBean.getData().getId());
                    serverTaskDataBean.setServerTaskType(taskBean.getTaskType());
                    serverTaskDataBean.setMainId(taskBean.getData().getMainId());
                }else if(taskBean.getTaskType()==WhatsAppTypeConfig.WHATS_TYPE_UPDATE_USER_INFO){
                    serverTaskDataBean.setTaskType(WhatsAppTypeConfig.WHATS_TYPE_UPDATE_USER_INFO);
                    serverTaskDataBean.setId(taskBean.getData().getId());
                    serverTaskDataBean.setServerTaskType(WhatsAppTypeConfig.WHATS_TYPE_UPDATE_USER_INFO);
                    serverTaskDataBean.setMainId(taskBean.getData().getMainId());
                }
            }
        }
        return serverTaskDataBean;
    }




    public static void openApplicationByWhatsTaskType(ServerTaskDataBean serverTaskDataBean) {
        BaseTask task = TaskManager.getInstance().getCurrentTask();
        if(task==null){
            return;
        }
        //无障碍服务开启成功时才执行对应指令
        String nowTaskPackageName = Constants.whatsAppPackageName;
        if (!AppUtils.Companion.isAppInstall(BaseApp.app.getApplicationContext(), Constants.whatsAppPackageName)) {
            TaskManager.getInstance().getCurrentTask().onTaskErr("未安装WhatsApp");
        } else {
            TaskManager.getInstance().onOpenTaskApp(task,nowTaskPackageName);
        }
    }


    /**
     * 根据任务类型打开app
     *
     * @param serverTaskDataBean
     */

    public static void openApplicationByTaskType(ServerTaskDataBean serverTaskDataBean) {
        BaseTask task = TaskManager.getInstance().getCurrentTask();
        if(task==null){
            showLog("task为空");
            return;
        }
        //无障碍服务开启成功时才执行对应指令
        String nowTaskPackageName = Constants.tiktokPackageName;
        if (!AppUtils.Companion.isAppInstall(BaseApp.app.getApplicationContext(), Constants.tiktokPackageName)) {
            TaskManager.getInstance().getCurrentTask().onTaskErr("未安装TikTok");
        } else {
            if(serverTaskDataBean.getTaskType()== TaskTypeConfig.TIKTOK_TASK_SINGLE_WORKS_COMMENT_BY_LINK
                    ||serverTaskDataBean.getTaskType()==TaskTypeConfig.TIKTOK_TASK_SINGLE_WORKS_LIKE_BY_LINK){
                TikTokTask.isJumpToWebOnce=false;
            }
            //增加自动注册任务接收到后先清除tiktok的缓存
            if(serverTaskDataBean.getTaskType()==TaskTypeConfig.TIKTOK_TASK_AUTO_REGISTER){
                dealWithAutoRegisterLauncher(task);
            }else {
                showLog("打开"+nowTaskPackageName);
                TaskManager.getInstance().onOpenTaskApp(task,nowTaskPackageName);
            }
        }
    }

}
